﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace YXShop.Web.admin.product
{
    public partial class product_batch_edite : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("001008004","对不起，您没有权限进行批量编辑");
                this.UnitBind();
                this.BindBrand();
                this.InitWebControl();
                this.ControlShow();
                YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                this.txtArea.Attributes.Add("readonly", "readonly");
                this.txtArea.Attributes.Add("onclick", "selectFile('Area',new Array(" + this.hfid.ClientID + "," + this.txtArea.ClientID + "),310,450,'" + sp.DummyPaht + "');");
                //this.txtFilingTime.Text = DateTime.Now.ToShortDateString();
                //ChangeHope.WebPage.WebControl.SetDate(this.txtFilingTime);
                //this.lblMemberRank.Text = BindRank("");
                //YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
                //this.txtArea.Attributes.Add("readonly", "readonly");
                //this.txtArea.Attributes.Add("onclick", "selectFile('Area',new Array(" + this.hfid.ClientID + "," + this.txtArea.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            }
        }

        #region 控制修改项
        private void ControlShow()
        {
            string filedStr = ChangeHope.WebPage.PageRequest.GetQueryString("field");
            string[] filed = filedStr.Split(',');
            for (int i = 0; i < filed.Length; i++)
            {
                switch (filed[i].ToString())
                {
                    case "1":
                        this.trProductBrand.Style.Value = "display:";
                        break;
                    case "2":
                        this.trProductUnit.Style.Value = "display:";
                        break;
                    case "3":
                        this.trProductWeight.Style.Value = "display:";
                        break;
                    case "4":
                        this.trProductIsSell.Style.Value = "display:";
                        this.hfIsSell.Value = "1";
                        break;
                    case "5":
                        this.trFilingTime.Style.Value = "display:";
                        break;
                    case "6":
                        this.trDownTime.Style.Value = "display:";
                        break;
                    case "7":
                        this.trFilingCount.Style.Value = "display:";
                        break;
                    case "8":
                        this.trDonateInteg.Style.Value = "display:";
                        break;
                    case "9":
                        this.trMarkePrice.Style.Value = "display:";
                        break;
                    case "10":
                        this.trCostPrice.Style.Value = "display:";
                        break;
                    case "11":
                        this.trAlarmCount.Style.Value = "display:";
                        break;
                    case "12":
                        this.trContentPageTemplate.Style.Value = "display:";
                        break;
                    case "13":
                        this.trState.Style.Value = "display:";
                        this.hfState.Value = "yes";
                        break;
                    case "14":
                        this.trMarking.Style.Value = "display:";
                        this.hfMarking.Value = "yes";
                        break;
                    case "15":
                        this.trStock.Style.Value = "display:";
                        break;
                    case "16":
                        this.trIntegralAmount.Style.Value = "display:";
                        break;
                    case "17":
                        this.trShopPrice.Style.Value = "display:";
                        break;
                    case "18":
                        this.trDescription.Style.Value = "display:";
                        break;
                    case "19":
                        this.trContent.Style.Value = "display:";
                        this.trContentTitle.Style.Value = "display:";
                        break;
                    case "20":
                        this.trArea.Style.Value = "display:";
                        break;
                }
            }
        }
        #endregion

        #region 验证
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtWeiht, "输入商品重量", "ifisfloat", "选填", "该项只能输入数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtStock, "输入商品库存数量", "ifisint", "选填", "该项只能输入数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtIntegralAmount, "积分购买额度", "ifisfloat", "选填", "该项只能输入数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtShopPrice, "输入销售价格", "ifisfloat", "选填", "该项只能输入数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtUpCount, "前台商品的可否购买状态取决于您的商品上架数量，可以小于或等于商品库存，请输入正整数", "ifisint", "选填", "该项必须为数字");
            ChangeHope.WebPage.WebControl.SetDate(this.txtFilingTime);
            ChangeHope.WebPage.WebControl.SetDate(this.txtDownTime);
            ChangeHope.WebPage.WebControl.Validate(this.txtFilingTime, "商品在前台展示的开始时间", "ifisnull", "选填", "该项为选填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtDownTime, "商品在前台展示的结束时间", "ifisnull", "选填", "该项为选填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtMarketPrice, "输入市场价格将作为商城价格与市场价格进行对比", "ifisfloat", "选填", "该项必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtCostPrice, "输入成本价将方便后台管理查看,不会在前台让用户看到该价格", "ifisfloat", "选填", "该项必须为数字");
            ChangeHope.WebPage.WebControl.Validate(this.txtDonateInteg, "用户购买该商品否可以获得的积分数，为0表示购买该商品不赠送积分", "ifisint", "选填", "该项只能输入数字");
            this.txtContentPageTemplate.Attributes.Add("onclick", "GetFile(this)");
            this.txtContentPageTemplate.Attributes.Add("readonly", "readonly");
            this.GetFileList();
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        #region 模板
        private void GetFileList()
        {
            /*
            修改人：ym
            修改时间：2009-8-26
            修改内容：添加
             */
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.Common.FileHelper file = new ChangeHope.Common.FileHelper();

            StringBuilder filelist = new StringBuilder();
            string isStoreLabel = ChangeHope.WebPage.PageRequest.GetString("isStoreLabel");
            if (isStoreLabel == "0")
            {
                file.rootUrl = Server.MapPath("~/" + sp.WebSiteTemplatePath);
            }
            else
            {
                file.rootUrl = Server.MapPath("~/templates/store_template");
            }

            file.listFiles(file.rootUrl, 0);

            filelist.AppendLine("<script type=\"text/javascript\">");
            filelist.AppendLine("d = new dTree('d');");
            filelist.AppendLine("d.add(0,-1,'请选择模版');");
            filelist.AppendLine(file.fileTree.ToString());
            filelist.AppendLine("document.write(d);");
            filelist.AppendLine("$(\"fileLists\").style.visibility=\"hidden\";");
            filelist.AppendLine("</script>");
            this.ltlFileList.Text = filelist.ToString();
            file = null;
        }
        #endregion

        #region 绑定单位信息
        protected void UnitBind()
        {
            YXShop.BLL.Product.ProductUnit bll = new YXShop.BLL.Product.ProductUnit();
            List<YXShop.Model.Product.ProductUnit> list = bll.GetAll(" 1=1");
            if (list.Count > 0)
            {
                this.ddlProUnit.Items.Add(new ListItem("请选择...", ""));
                for (int i = 0; i < list.Count; i++)
                {
                    this.ddlProUnit.Items.Add(new ListItem(list[i].Name, list[i].ID.ToString()));
                }
            }
        }
        #endregion

        #region 绑定商品品牌
        protected void BindBrand()
        {
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            List<YXShop.Model.Product.ProductBrand> brandList = bll.GetDTList("");
            if (brandList.Count > 0)
            {
                this.ddlProductBrand.Items.Add(new ListItem("请选择...", "0"));
                for (int i = 0; i < brandList.Count; i++)
                {
                    this.ddlProductBrand.Items.Add(new ListItem(brandList[i].Name, brandList[i].ID.ToString()));
                }
            }
        }
        #endregion

        protected void ltbSave_Click(object sender, EventArgs e)
        {
            string proIdList = ChangeHope.WebPage.PageRequest.GetQueryString("productId");
            string[] arrProId = proIdList.Split(',');
            for (int i = 0; i < arrProId.Length; i++)
            {
                YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
                YXShop.Model.Product.ProductInfo model = bll.GetModel(Convert.ToInt32(arrProId[i]));
                if (model != null)
                {
                    #region 更新字段
                    if (!string.IsNullOrEmpty(this.hfid.Value.Trim()))
                    {
                        model.pro_Area = this.hfid.Value.Trim();
                    }
                    if (this.ddlProductBrand.SelectedValue != "0" && !string.IsNullOrEmpty(this.ddlProductBrand.SelectedValue))
                    {
                        model.pro_BrandID = Convert.ToInt32(this.ddlProductBrand.SelectedValue);
                    }
                    if (!string.IsNullOrEmpty(this.ddlProUnit.SelectedValue))
                    {
                        model.pro_Unit = this.ddlProUnit.SelectedValue;
                    }
                    if (!string.IsNullOrEmpty(this.txtWeiht.Text.Trim()))
                    {
                        model.pro_Weight = Convert.ToDecimal(this.txtWeiht.Text.Trim());
                    }
                    if (!string.IsNullOrEmpty(this.hfIsSell.Value))
                    {
                        model.pro_Audit = Convert.ToInt32(this.rblIsShelves.SelectedValue);
                    }
                    if (!string.IsNullOrEmpty(this.txtFilingTime.Text.Trim()))
                    {
                        model.pro_AutoUp = this.txtFilingTime.Text.Trim() != "" ? Convert.ToDateTime(this.txtFilingTime.Text.Trim()) : System.DateTime.Now;
                    }
                    if (!string.IsNullOrEmpty(this.txtDownTime.Text.Trim()))
                    {
                        model.pro_AutoDown = this.txtDownTime.Text.Trim() != "" ? Convert.ToDateTime(this.txtDownTime.Text.Trim()) : Convert.ToDateTime("2020-01-01");
                    }
                    if (!string.IsNullOrEmpty(this.txtUpCount.Text.Trim()))
                    {
                        model.pro_AuditCount = this.txtUpCount.Text.Trim() != "" ? Convert.ToInt32(this.txtUpCount.Text.Trim()) : 0;
                    }
                    if (!string.IsNullOrEmpty(this.txtDonateInteg.Text.Trim()))
                    {
                        model.pro_DonateIntegral = this.txtDonateInteg.Text.Trim() != "" ? Convert.ToDecimal(this.txtDonateInteg.Text.Trim()) : 0;
                    }
                    if (!string.IsNullOrEmpty(this.txtMarketPrice.Text.Trim()))
                    {
                        model.pro_MarketPrice = Convert.ToDecimal(this.txtMarketPrice.Text.Trim());
                    }
                    if (!string.IsNullOrEmpty(this.txtCostPrice.Text.Trim()))
                    {
                        model.pro_CostPrice = this.txtCostPrice.Text.Trim() != "" ? Convert.ToDecimal(this.txtCostPrice.Text.Trim()) : 0;
                    }
                    if (!string.IsNullOrEmpty(this.txtAlarm.Text))
                    {
                        model.pro_Alarm = this.txtAlarm.Text != "" ? Convert.ToInt32(this.txtAlarm.Text) : 0;
                    }
                    if (!string.IsNullOrEmpty(this.txtContentPageTemplate.Text.Trim()))
                    {
                        model.pro_ContentTemplate = this.txtContentPageTemplate.Text.Trim();
                    }
                    #region 商品状态：推荐商品|热卖商品|特价商品|活动专区|最新商品|首页显示
                    if (!string.IsNullOrEmpty(this.hfState.Value.Trim()))
                    {
                        string Pro_State = "";
                        for (int s = 0; s < this.chklstState.Items.Count; s++)
                        {
                            if (chklstState.Items[s].Selected)
                            {
                                Pro_State += "1|";
                            }
                            else
                            {
                                Pro_State += "0|";
                            }
                        }
                        model.pro_State = Pro_State;
                    }
                    #endregion
                    
                    #region 商品标识：团购|拍卖|批发
                    if (!string.IsNullOrEmpty(this.hfMarking.Value.Trim()))
                    {
                        string Pro_Designation = "";
                        for (int j = 0; j < this.chklstMarking.Items.Count; j++)
                        {
                            if (chklstMarking.Items[j].Selected)
                            {
                                Pro_Designation += "1|";
                            }
                            else
                            {
                                Pro_Designation += "0|";
                            }
                        }
                        model.pro_Designation = Pro_Designation;
                    }
                    #endregion

                    if (!string.IsNullOrEmpty(this.txtStock.Text.Trim()))
                    {
                        model.pro_Stock =Convert.ToInt32(this.txtStock.Text.Trim());
                    }
                    if (!string.IsNullOrEmpty(this.txtIntegralAmount.Text.Trim()))
                    {
                        model.pro_IntegralPrice =Convert.ToDecimal(this.txtIntegralAmount.Text.Trim());
                    }
                    if (!string.IsNullOrEmpty(this.txtShopPrice.Text.Trim()))
                    {
                        model.pro_ShopPrice =Convert.ToDecimal(this.txtShopPrice.Text.Trim());
                    }
                    if (!string.IsNullOrEmpty(this.txtDescription.Text.Trim()))
                    {
                        model.pro_Synopsis = this.txtDescription.Text;
                    }
                    if (!string.IsNullOrEmpty(this.txtContent.Value.Trim()))
                    {
                        model.pro_Content = this.txtContent.Value;
                    }
                    #endregion
                    model.pro_UpdateTime = DateTime.Now;
                    bll.Update(model);
                }

            }
            ChangeHope.WebPage.BasePage.PageRight("更新成功，共更新" + arrProId.Length.ToString() + "条数据", "product_list.aspx?q_PutoutType=0");
       }
    }
}
